<?php

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

session_start();
$_SESSION['HTTP_USER_AGENT'] = md5($_SERVER['HTTP_USER_AGENT']);

require_once('../services/webservices.php');
require_once('random_generator.php');


// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];


 $members = new members();
 $members->username = $myusername;
 $members->password = $mypassword;
 $getResponse = $members->authenticate();
 $xml = new SimpleXMLElement($getResponse);
 echo $count = $xml->Item_Count; 
    


// If result matched $myusername and $mypassword, must be only one match

if($count==1)
{

    
     $getResponse = $members->adduserhistory();
     $xml = new SimpleXMLElement($getResponse);
     $inserted = $xml->Item_Id;
     if($inserted > 0)
     {
         $random = new RandomAlphaNumeric();
         $rand_string = $random->str_rand(30, 'alphanum'); 
    
        setcookie('token', $rand_string);
   
        $_SESSION["token"]= $rand_string;
        $_SESSION['username'] = $myusername;
        $_SESSION['password'] = $mypassword;
        header("location:main.php");
       
     }
     else
     {
        header("location:main_login.php");   
         
     }
     
     
   
}
else 
{   
    header("location:main_login.php");
   
}



?>